package baidu.com.matedemo.login;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.baidu.duer.libcore.util.ToastUtil;
import com.baidu.duer.smartmate.out.BaiduOauthToken;
import com.baidu.duer.smartmate.out.DuerSDK;
import com.baidu.duer.smartmate.out.oauth.ILoginCallback;
import com.baidu.duer.smartmate.out.oauth.IResponseWithParamCallback;
import com.baidu.duer.smartmate.out.oauth.LogoutAccountCallback;

import java.util.LinkedHashMap;

import baidu.com.matedemo.R;
import baidu.com.matedemo.base.BaseActivity;

/**
 * Created by lihui38 on 2017/11/2.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private final static String LOG_TAG = "BaiduILoginAdaptorImpl";

    private Button btnLogin;
    private Button btnIsLogin;
    private Button btnLogout;
    private Button btnPassportSdkLogin;
    private Button btnPassportSdkOauth;

    @Override
    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.login, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(View view) {
        super.onContentViewCreated(view);

        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
        btnIsLogin = (Button) findViewById(R.id.btn_is_login);
        btnIsLogin.setOnClickListener(this);
        btnLogout = (Button) findViewById(R.id.btn_logout);
        btnLogout.setOnClickListener(this);
        btnPassportSdkLogin = (Button) findViewById(R.id.btn_passport_sdk_login);
        btnPassportSdkLogin.setOnClickListener(this);
        btnPassportSdkOauth = (Button) findViewById(R.id.btn_passport_sdk_oauth);
        btnPassportSdkOauth.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(btnLogin)) {
            DuerSDK.login(this, new ILoginCallback() {
                @Override
                public void onCancel() {
                    ToastUtil.toastWarnning(LoginActivity.this, "已取消登录");
                }

                @Override
                public void onError(int code, String message) {
                    Log.e(LOG_TAG, "errorCode:" + code + " msg:" + message);
                    ToastUtil.toastWarnning(LoginActivity.this, message);
                }

                @Override
                public void onCompleted() {
                    ToastUtil.toastSuccess(LoginActivity.this, "登录成功");
                }
            });
        } else if (v.equals(btnIsLogin)) {
            if (DuerSDK.isLogin()) {
                ToastUtil.toastSuccess(this, "已登录");
            } else {
                ToastUtil.toastWarnning(this, "未登录");
            }
        } else if (v.equals(btnLogout)) {
            DuerSDK.logout(this, new LogoutAccountCallback() {
                @Override
                public void onComplete() {
                    ToastUtil.toastSuccess(LoginActivity.this, "注销成功");
                }
            });
        } else if (v.equals(btnPassportSdkLogin)) {
            LinkedHashMap<String, String> agreement = new LinkedHashMap<>();
            agreement.put("度秘协议", "https://www.baidu.com");
            agreement.put("苏宁协议", "http://dev.passport.baidu.com/home/index");
            DuerSDK.passportSdkLogin(agreement);
        } else if (v.equals(btnPassportSdkOauth)) {
            String clientId = "Bd9rtoM39XmYIesLpaAVX19jBAcF4gWq";
            DuerSDK.passportSdkOauth(clientId, new IResponseWithParamCallback<BaiduOauthToken>() {
                @Override
                public void onSuccess(BaiduOauthToken token) {
                    ToastUtil.toastSuccess(LoginActivity.this, "授权成功:" + token.getAccessToken());
                }

                @Override
                public void onError(long status, String msg) {
                    ToastUtil.toastWarnning(LoginActivity.this, "授权失败:" + msg);
                }
            });
        }
    }
}
